<?php



namespace Home\Controller;



use Think\Controller;



class ShowsController extends Controller {

	private $access_token='';
	public function demo() {

		// 设置选项，包括URL

		$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . appid . "&redirect_uri=http://nuower.com/index.php/Shows/oauth&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
		header("Location:".$url);

	}

	public function oauth() {
		//$this->display ( "Shows/second" );exit();

		if (isset ( $_GET ['code'] )) {

			$code = $_GET ['code'];

			$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . appid . "&secret=" . appsecret . "&code=" . $code . "&grant_type=authorization_code";

			$output = https_request ( $url );

			// 第二步

			if (isset ( $output )) {

				$jsoninfo = json_decode ( $output, true );
				$url_info = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $jsoninfo ['access_token'] . "&openid=" . $jsoninfo ['openid'] . "&lang=zh_CN";

				$userInfo = https_request ( $url_info );
			
			} else {

				$this->error ( "不好意思,出错了" );

			}

			// 第三步

			if (isset ( $userInfo )) {

				$res = json_decode ( $userInfo, true );

				session ( "openid", $res ['openid'] );

				session ( "nickname", $res ['nickname'] );

				$this->display ( "Shows/second" );

			} else {

				$this->error ( "不好意思,出错了" );

			}

		} else {

			$this->error ( "不好意思,出错了" );

		}

	}

	public function second() {

		$data = F ( 'data' );

		if (empty ( $data )) {

			$data = M ( 'swf_area' )->select ();

			$cache = F ( 'data', $data );

			$this->assign ( "city", $cache );

		} else {

			// dump($data);

			$this->assign ( "city", $data );

		}

		$this->display ( "second" );

	}
	
	public function third() {
		header ( "Content-type: text/html; charset=utf-8" );
		$data = I ();	
		$jiaju = $data ['jiaju'];
		
		$jiajudata = explode('、',$jiaju);
		for ($i=0;$i<count($jiajudata)-1;$i++){ 
			$tmp=explode(' ', $jiajudata[$i]);
			$list[] = array(
				'name'=>$tmp[0],
				'num'=>$tmp[1],
			);
		}
		
		if(!empty($_REQUEST['shighdianti'])){  
			$startAdd=$data['startAdd']."有电梯";
		}else{
			$startAdd=$data['startAdd'].$data['shigh']."楼";	
		}
		
		if(!empty($_REQUEST['ehighdianti'])){  
			$endAdd=$data['endAdd']."有电梯";
		}else{
			$endAdd=$data['endAdd'].$data['ehigh']."楼";	
		}
		
		
		//添加到数据库
		$data1['getCity'] = $data['getCity'];
		$data1['startAdd'] = $startAdd;
		$data1['endAdd'] = $endAdd;
		$data1['orderno'] = $this->ordersNum ();
		$data1['ctime'] = date ( 'Y-m-d H:i:s' );
		$data1['car'] = $data['car'];
		$data1['name'] = $data['name'];
		$data1['mphone'] = $data['mphone'];
		$data1['yphone'] = $data['yphone'];
		$data1['cost'] = $data['cost'];
		
		$data1['openid'] = $_SESSION ['openid'];
		$data1['items'].="行李数".$data['count-input']."件<br/>大件家具：".$data['jiaju']."<br/>其他家居：".$data['ben'];
		
		$res = M ( 'orders' )->data ( $data1 )->add ();

		if ($res) {
			
			// 模板
			//$tpl = "订购单号:<span style='color:red'>" . $data ['orderno'] . "</span><br>下单时间:<span style='color:red'>" . $data ['ctime'] . "</span><br>收到来自<span style='color:red'>" . $data ["getCity"] . "</span>的订单。总金额为:<span style='color:red'>" . $data ['cost'] . "</span><br>" . "联系人:<span style='color:red'>" . $data ['name'] . "</span><br>联系电话:<span style='color:red'>" . $data ['mphone'] . "</span><br>选择车型:<span style='color:red'>" . $data ['car'] . "</span><br>" . "搬家时间:<br><span style='color:red'>" . $appDate. " " . $appTime . "</span><br>" . "起始地：<br><span style='color:red'>" . $data ['startAdd'].$shigh . "楼</span><br>目的地：<br><span style='color:red'>" . $data ['endAdd'].$ehigh . "楼</span><br>搬运物品：<span style='color:red'>".$data ['items']."</span>";
//			SendMail ( '187220366@qq.com', '亲，又来新订单了！！', $tpl );
			
			$this->assign ( "orderno", $data1['orderno']);
			$this->assign ( "jianshu", $data ['count-input']);
			$this->assign ( "car", $data ['car']);
			$this->assign ( "cost", $data ['cost']);
			$this->assign ( "getCity", $data ['getCity']);
			
			$this->assign ( "list", $list);
			$this->assign ( "jiaju", $data ['jiaju']);
			
			$this->assign ( "name", $data ['name']);
			$this->assign ( "mphone", $data ['mphone'] );
			$this->assign ( "appDate", $data ['appDate']);
			$this->assign ( "startAdd", $startAdd );
			$this->assign ( "endAdd", $endAdd );
			$this->assign ( "beizhu", $data ['beizhu']);
			$this->display ( "detail" );
			
		}else{
			$this->error ( "订单失败了！" );	
		}
		//echo $data ['getCity'];
//		echo $data ['cost'];
//		echo $data ['count-input'];
//		exit;
		
		

	}
	
	
	

	// 订单 需要搬运

	public function orders() {

		header ( "Content-type: text/html; charset=utf-8" );

		// 解释下参数: 参数1---目标邮箱, 参数2----邮件标题,参数三--邮件正文,参数四---发件人名称;

		$data = I ();

		//dump($data);


		$data ['orderno'] = $this->ordersNum ();

		$data ['ctime'] = date ( 'Y-m-d H:i:s' );

		$data ['openid'] = $_SESSION ['openid'];

		session("name",$data ['name']);

		session("orderno",$data ['orderno']);

		unset ( $data ['options'] );

		unset ( $data ['costFb'] );

		$data ['time'] = $data ['appDate'] . $data ['appTime'];

		$appTime = $data ['appTime'];

		$appDate = $data ['appDate'];

		$shigh =  $data ['shigh'];

		$ehigh=  $data ['ehigh'];
		
		$data['items'].="行李数".$data['count-input']."件<br/>大件家具：".$data['jiaju']."<br/>其他家居：".$data['ben'];

		unset ( $data ['ben'] );

		unset ( $data ['appTime'] );

		unset ( $data ['appDate'] );

		unset ( $data ['shigh'] );

		unset ( $data ['ehigh'] );

		 

		$res = M ( 'orders' )->data ( $data )->add ();

		if ($res) {

			// 如果发送邮件推送成功并把信息保存到数据库并显示给用户

			// 模板

			$tpl = "订购单号:<span style='color:red'>" . $data ['orderno'] . "</span><br>下单时间:<span style='color:red'>" . $data ['ctime'] . "</span><br>收到来自<span style='color:red'>" . $data ["getCity"] . "</span>的订单。总金额为:<span style='color:red'>" . $data ['cost'] . "</span><br>" . "联系人:<span style='color:red'>" . $data ['name'] . "</span><br>联系电话:<span style='color:red'>" . $data ['mphone'] . "</span><br>选择车型:<span style='color:red'>" . $data ['car'] . "</span><br>" . "搬家时间:<br><span style='color:red'>" . $appDate. " " . $appTime . "</span><br>" . "起始地：<br><span style='color:red'>" . $data ['startAdd'].$shigh . "楼</span><br>目的地：<br><span style='color:red'>" . $data ['endAdd'].$ehigh . "楼</span><br>搬运物品：<span style='color:red'>".$data ['items']."</span>";

			if (SendMail ( 'wxd1277@163.com', '亲，又来新订单了！！', $tpl )) {

					

			} else {

				$this->error ( '订单失败了！' );

			}

			$this->assign ( "tpl", $tpl );

			$this->display ( "detail" );

		} else {

			$this->error ( "订单失败了！" );

		}

		

	}

	// 取消订单

	public function delOrder() {

		$data ['openid'] = $_SESSION ['openid'];

		$name = $_SESSION['name'];

		$ordNo = $_SESSION['orderno'];

		$tpl = $name."(".$_SESSION['nickname'].")"."取消订单了"."单号为:". $ordNo;

		 

		$where = array (

				"openid" => $data ['openid'] ,

				"orderno" => $ordNo

		);

		//0代表以删除订单

		$status['status'] = '0';

		$res = M ( 'orders' )->where ( $where )->save($status);; // 删除用户数据

		if ($res) {

			if (SendMail ( 'nuowoer@163.com', '亲，有人取消订单了！', $tpl )) {

				$this->success("取消订单成功！",U ( 'Show/index' ));

			} else {

				$this->error ( "取消订单失败",U ( 'Show/index' ) );

			}

		} else {

			$this->error ( "取消订单失败",U ( 'Show/index' ) );

		}

	}

	// 单号

	public function ordersNum() {

		$data = date ( 'Ymd' );

		$whe = "%" . $data . '%';

		$where ['orderno'] = array (

				'like',

				$whe

		);

		$res = M ( 'orders' )->where ( $where )->order ( 'id desc' )->getField ( 'orderno' );

		// 如果存在

		if ($res) {

			$order = substr ( $res, - 4 ) + 1;

			$orderNo = 'C' . $data . sprintf ( "%04d", $order ); // 生成4位数，不足前面补0

		} else {

			// 否则单号就是0001

			$orderNo = 'C' . $data . "0001";

		}

		return $orderNo;

	}

	/**

	 * 需要搬运的算法

	 * $Fc车费

	 */

	public function banYun() {

		header ( "Content-type: text/html; charset=utf-8" );

		$dst = $_POST ['dst'];

		$Fb = $_POST ['fb'];

		// $domain = substr(strstr($dst, "分钟"),6);
        //$datastr = explode("公里",$dst);
		
		$res = substr ( $dst, 0, strlen ( $dst ) - 6 );

		$car = $_POST ['car'];

		// 调用计算车费函数

		$Fc = $this->carCost ( $res, $car );

		$F = $Fc+$Fb;

		echo $F.'|'.$Fc;

	}

	/**

	 * *

	 * 车费计算公式

	 * $res距离,$car车型

	 *

	 * *

	 */

	private function carCost($res, $car) {

		switch ($car) {

			case "面包车" :

				// 起步价

				$Fa = 35;

				if ($res > 5) {

					$cost = round(35 + ($res - 5) * 4);

				} else {

					$cost = 35;

				}

				break;

			case "小型平板货车" :

				// 起步价

				$Fa = 60;

				if ($res > 5) {

					$cost = round(60 + ($res - 5) * 5);

				} else {

					$cost = 60;

				}

				break;

			case "中型平板货车" :

				// 起步价

				$Fa = 80;

				if ($res > 5) {

					$cost = round(80 + ($res - 5) * 5);

				} else {

					$cost = 80;

				}

				break;

			case "中型厢式货车" :

				// 起步价

				$Fa = 100;

				if ($res > 5) {

					$cost = round(100 + ($res - 5) * 5);

				} else {

					$cost = 100;

				}

				break;

			default :

				// 起步价

				$Fa = 35;

				if ($res > 5) {

					$cost = round(35 + ($res - 5) * 4);

				} else {

					$cost = 35;

				}

				break;

		}

		return $cost;

	}


	//发送模板消息
	public function sendtpl_msg($touser,$orderno,$cost){
$json_token=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".appid."&secret=".appsecret);
$access_token=json_decode($json_token,true);
//获得access_token
$this->access_token=$access_token['access_token'];
//echo $this->access_token;exit;
//模板消息	
$template=array(
'touser'=>$touser,
'template_id'=>"GrY-eEigqO2iMVwpm4KfO9G8N3FEYx_m004HMD68hXc",
'url'=>"http://nuower.com/nw.php",
'topcolor'=>"#7B68EE",
'data'=>array(
'first'=>array('value'=>urlencode("管理员您好,已有客户提交订单,请及时操作"),'color'=>"#743A3A"),
'orderno'=>array('value'=>urlencode($orderno),'color'=>'#EEEEEE'),
'amount'=>array('value'=>urlencode($cost),'color'=>'#EEEEEE'),
'remark'=>array('value'=>urlencode('请及时审核'),'color'=>'#FFFFFF'),
)
);
$json_template=json_encode($template);
//echo $json_template;
//echo $this->access_token;
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;
$res=http_request($url,urldecode($json_template));
if ($res[errcode]==0) echo '模板消息发送成功!';
//print_r($res);

}

public function submit_order()
{
	$id=I('get.id');
	$orderinfo=M('orders')->find($id);
	$this->sendtpl_msg('oB_Kewa9G5LMhGzLCpQKePUz7EkA',$orderinfo['orderno'],$orderinfo['cost']);
}



}



?>